import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ytbusiness/config/style.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';


//修改邮箱
class ModifyMailbox extends StatefulWidget {
  @override
  _ModifyMailboxState createState() => _ModifyMailboxState();
}

class _ModifyMailboxState extends State<ModifyMailbox> {
  Timer _countdownTimer;//倒计时
  int _countdownNum = 60;//倒计时60s
  bool bindEMail = true;
  TextEditingController _code= TextEditingController();//验证码
  TextEditingController _newEmail = TextEditingController();//新邮箱
  TextEditingController _newCode= TextEditingController();//新验证码
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        backgroundColor: YTStyle.bgColor,
        appBar: customAppBar(context,title: "修改邮箱"),
        body: SingleChildScrollView(
          child: bindEMail?Column(
            children: [
              SizedBox(
                height: setWidth(15),
              ),
              Container(
                color: Colors.white,
                padding: EdgeInsets.only(top: setWidth(23),left: setWidth(16),right: setWidth(16),bottom: setWidth(23)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Text("原邮箱",style: TextStyle(color: Color(0xff333333),fontSize: setSp(15)),),
                    ),
                    Container(
                      child: Text("135@123.com",style: TextStyle(color: Color(0xff666666),fontSize: setSp(15)),),
                    )
                  ],

                ),
              ),
              Container(
                color: Colors.white,
                height: setWidth(60),
                padding: EdgeInsets.only(left: setWidth(16),right: setWidth(16)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      child: TextField(
                        controller: _code,
                        textAlign: TextAlign.left,
                        style: TextStyle(fontSize: setSp(16)),
                        maxLines: 1,
                        textInputAction: TextInputAction.done,
                        onSubmitted: (str) {},
                        onChanged: (val){
                          setState(() {

                          });
                        },
                        decoration: InputDecoration(
                          hintText: '请输入验证码',

                          isDense: true,
                          border: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          enabledBorder: InputBorder.none,
                        ),
                      ),
                    ),
                    SizedBox(
                      width: setWidth(10),
                    ),
                    GestureDetector(
                      onTap: (){
                        _countdown();
                      },
                      child: Container(
                        height: setWidth(34),
                        width: setWidth(90),
                        alignment: Alignment.center,
                       decoration: BoxDecoration(
                         color: YTTheme.theme,
                         borderRadius: BorderRadius.circular(3)
                       ),
                        child: Text(_countdownNum == 60?"获取验证码":"${_countdownNum}s",style: TextStyle(color: Colors.white,fontSize: setSp(12)),),
                      ),
                    ),
                  ],

                ),
              ),
              SizedBox(
                height: setWidth(73),
              ),
              InkWell(
                onTap: (){
                  setState(() {
                    _countdownNum = 60;
                    _countdownTimer?.cancel();
                    _countdownTimer = null;
                    bindEMail = false;
                  });
                },
                child: Container(
                  margin:EdgeInsets.only(left: setWidth(15),right: setWidth(15)) ,
                  height: setWidth(44),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: YTTheme.theme,
                      borderRadius: BorderRadius.circular(3)
                  ),
                  child: Text("下一步",style: TextStyle(color: Colors.white,fontSize: setSp(15)),),
                ),
              )
            ],
          ):
          Column(
            children: [
              SizedBox(
                height: setWidth(15),
              ),
              Container(
                color: Colors.white,
                height: setWidth(60),
                alignment: Alignment.center,
                padding: EdgeInsets.only(left: setWidth(16),right: setWidth(16)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      child: Text("原邮箱",style: TextStyle(color: Color(0xff333333),fontSize: setSp(15)),),
                    ),
                    SizedBox(
                      width: setWidth(10),
                    ),
                    Expanded(
                      child: TextField(
                        controller: _newEmail,
                        textAlign: TextAlign.right,
                        style: TextStyle(fontSize: setSp(16)),
                        maxLines: 1,
                        textInputAction: TextInputAction.done,
                        onSubmitted: (str) {},
                        onChanged: (val){
                          setState(() {

                          });
                        },
                        decoration: InputDecoration(
                          hintText: '请输入新邮箱',

                          isDense: true,
                          border: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          enabledBorder: InputBorder.none,
                        ),
                      ),
                    ),
                  ],

                ),
              ),
              Container(
                color: Colors.white,
                height: setWidth(60),
                padding: EdgeInsets.only(left: setWidth(16),right: setWidth(16)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      child: TextField(
                        controller: _newCode,
                        textAlign: TextAlign.left,
                        style: TextStyle(fontSize: setSp(16)),
                        maxLines: 1,
                        textInputAction: TextInputAction.done,
                        onSubmitted: (str) {},
                        onChanged: (val){
                          setState(() {

                          });
                        },
                        decoration: InputDecoration(
                          hintText: '请输入验证码',

                          isDense: true,
                          border: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          enabledBorder: InputBorder.none,
                        ),
                      ),
                    ),
                    SizedBox(
                      width: setWidth(10),
                    ),
                    GestureDetector(
                      onTap: (){
                        _countdown();
                      },
                      child: Container(
                        height: setWidth(34),
                        width: setWidth(90),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: YTTheme.theme,
                            borderRadius: BorderRadius.circular(3)
                        ),
                        child: Text(_countdownNum == 60?"获取验证码":"${_countdownNum}s",style: TextStyle(color: Colors.white,fontSize: setSp(12)),),
                      ),
                    ),
                  ],

                ),
              ),
              SizedBox(
                height: setWidth(73),
              ),
              Container(
                margin:EdgeInsets.only(left: setWidth(15),right: setWidth(15)) ,
                height: setWidth(44),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: YTTheme.theme,
                    borderRadius: BorderRadius.circular(3)
                ),
                child: Text("更换完成",style: TextStyle(color: Colors.white,fontSize: setSp(15)),),
              )
            ],
          )
        ),

      ),
    );
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _countdownTimer?.cancel();
  }
  //发送验证码 倒计时
  _countdown(){
//    if(_code.text.toString().trim() == ""){
//      toast("请输入手机号码");
//      return;
//    }

    if (_countdownTimer != null) {
      return;
    }
    //获取验证码
    _getCheckCode();


  }
  _getCheckCode(){
    //     开启倒计时
    _countdownTimer = Timer.periodic(new Duration(seconds: 1), (timer) {
      setState(() {
        if (_countdownNum > 0) {
          _countdownNum -= 1;
          print(_countdownNum);
        } else {
          _countdownNum = 60;
          _countdownTimer.cancel();
          _countdownTimer = null;
        }
      });
    });

  }
}
